package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.dto.EmpDto;
import com.jiyun.entity.Emp;
import com.jiyun.entity.Kaoqin;
import com.jiyun.service.IEmpService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-02-27
 */
@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    IEmpService empService;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addEmp(@RequestBody Emp emp){
        empService.save(emp);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateEmp(@RequestBody Emp emp){
        empService.updateById(emp);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        empService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        empService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public Emp findById(@RequestParam Integer id){
        return empService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<Emp> findAll(){
        return empService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<EmpDto> findPage(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestBody EmpDto empDto){

        return empService.findPage(page,pageSize,empDto);
    }

    @RequestMapping("login")
    public Object login(@RequestBody Emp emp){
        LambdaQueryWrapper<Emp> empLambdaQueryWrapper = new LambdaQueryWrapper<>();
        empLambdaQueryWrapper.eq(Emp::getWorknum,emp.getWorknum());
        Emp empDB = empService.getOne(empLambdaQueryWrapper);
        if (empDB==null) {
            return "工号不正确";
        }
        if(emp.getIdcard().length()!=6){
            return "身份证后六位不正确";
        }
        if (!empDB.getIdcard().endsWith(emp.getIdcard())) {
            return "身份证后六位不正确";
        }
        return empDB;
    }


    @RequestMapping("findNewWorknum")
    public String findNewWorknum(){

        String currMaxWorknum = empService.findMaxIdData();

        int num = Integer.parseInt(currMaxWorknum)+1;
        String newOrderId = "";
        if(num<10){
            newOrderId = "000"+num;
        }else if(num<100){
            newOrderId = "00"+num;
        }else if(num<1000){
            newOrderId = "0"+num;
        }

        return newOrderId;
    }

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("findEmpKaoqinDetail")
    public List<Map> findEmpKaoqinDetail(@RequestBody EmpDto empDto){


        System.out.println(empDto);

        List<Map> list = restTemplate.postForObject("http://localhost:8888/kaoqin/findEmpKaoqinDetail", empDto, List.class);

        return list;

    }

    @RequestMapping("exportDetail")
    public void exportDetail(@RequestBody EmpDto empDto, HttpServletResponse response) throws Exception{


        List<Map> list = restTemplate.postForObject("http://localhost:8888/kaoqin/findEmpKaoqinDetail", empDto, List.class);

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();


        int rowNum = 0;
        XSSFRow row = sheet.createRow(rowNum++);
        int cellNum = 0;
        XSSFCell cell = row.createCell(cellNum++);
        cell.setCellValue("工号");
        cell = row.createCell(cellNum++);
        cell.setCellValue("姓名");
        cell = row.createCell(cellNum++);
        cell.setCellValue("日期");
        cell = row.createCell(cellNum++);
        cell.setCellValue("考勤批次");
        cell = row.createCell(cellNum++);
        cell.setCellValue("规定时间");
        cell = row.createCell(cellNum++);
        cell.setCellValue("打卡时间");
        cell = row.createCell(cellNum++);
        cell.setCellValue("状态");


        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm:ss");




        for (Map map : list) {
            row = sheet.createRow(rowNum++);
            cellNum = 0;
            cell = row.createCell(cellNum++);
            cell.setCellValue(map.get("worknum").toString());
            cell = row.createCell(cellNum++);
            cell.setCellValue(map.get("name").toString());
            cell = row.createCell(cellNum++);
            cell.setCellValue(sdf1.format(new Date(Long.parseLong(map.get("workDate").toString()))));
            cell = row.createCell(cellNum++);
            cell.setCellValue(map.get("type").toString());
            cell = row.createCell(cellNum++);
            cell.setCellValue(sdf3.format(new Date(Long.parseLong(map.get("rule").toString()))));
            cell = row.createCell(cellNum++);
            cell.setCellValue(map.get("dakaTime")==null?"没有数据":sdf2.format(new Date(Long.parseLong(map.get("dakaTime").toString()))));
            cell = row.createCell(cellNum++);
            cell.setCellValue(map.get("result").toString());
        }

        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("考勤明细.xls", "UTF-8"));
        response.setHeader("Connection", "close");
        response.setHeader("Content-Type", "application/octet-stream");
        workbook.write(response.getOutputStream());

    }

    @RequestMapping("countDetail")
    public List<Map> countDetail(@RequestBody EmpDto empDto) throws Exception{
        List<Map> list = restTemplate.postForObject("http://localhost:8888/kaoqin/findEmpKaoqinDetail", empDto, List.class);

        int zhengchang = 0;
        int chidao = 0;
        int zaotui = 0;
        int weidaka = 0;
        for (Map map : list) {
            if (map.get("result").toString().equals("正常")) {
                zhengchang++;
            }
            if (map.get("result").toString().equals("迟到")) {
                chidao++;
            }
            if (map.get("result").toString().equals("早退")) {
                zaotui++;
            }
            if (map.get("result").toString().equals("未打卡")) {
                weidaka++;
            }
        }

        ArrayList<Map> list1 = new ArrayList<>();
        HashMap hashMap = new HashMap();
        hashMap.put("name","正常");
        hashMap.put("value",zhengchang);
        list1.add(hashMap);

        hashMap = new HashMap();
        hashMap.put("name","迟到");
        hashMap.put("value",chidao);
        list1.add(hashMap);

        hashMap = new HashMap();
        hashMap.put("name","早退");
        hashMap.put("value",zaotui);
        list1.add(hashMap);

        hashMap = new HashMap();
        hashMap.put("name","未打卡");
        hashMap.put("value",weidaka);
        list1.add(hashMap);

        return list1;
    }


    @RequestMapping("addDaka")
    public void addDaka(@RequestBody Kaoqin kaoqin) throws Exception{
        restTemplate.postForObject("http://localhost:8888/kaoqin/addDaka", kaoqin, Object.class);
    }



}
